<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2018 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 老猫 <zxxjjforever@163.com>
// +----------------------------------------------------------------------
namespace app\blog\controller;

use app\blog\model\JobModel;
use app\blog\model\PartyModel;
use app\blog\model\RoleuserModel;
use app\blog\model\UserModel;
use app\blog\model\BusinessModel;
use app\blog\model\StoreModel;

use cmf\controller\AdminBaseController;
use think\Db;
class PersonnelController extends AdminBaseController
{
    public function index(){

        return $this->fetch();
    }

    function add(){
        $user=DB::name('user')->select();
        $this->assign('user',$user);
        //var_dump($user);exit;
       /*$store_name=DB::name('store')
            ->alias('s')
            ->join('user u','u.storeid=s.id')
            ->column('s.store_name');
        //var_dump($store_name);exit;
        $this->assign('store_name',$store_name);*/

        return $this->fetch();

    }

    function adds(){
       $store_name=DB::name('store')->column('store_name');
        //var_dump($store_name);
        $this->assign('store_name',$store_name);

       $name=DB::name('role')->select();
        //var_dump($name);exit;
        $this->assign('name',$name);

        $job=DB::name('job')->field('depart')->select();

        $this->assign('job',$job);

        $job1=DB::name('job')->field('duty')->select();

        $this->assign('job1',$job1);


        return $this->fetch();

    }


    function append(){
        if($this->request->isPost()){
            //session_start();
            $data=$this->request->Post();
            $idnumber=DB::name('user')->where('idnumber',$data['idnumber'])->find();
            $user_email=DB::name('user')->where('user_email',$data['user_email'])->find();
            $mobile=DB::name('user')->where('mobile',$data['mobile'])->find();
            $job_num=DB::name('user')->where('job_num',$data['job_num'])->find();
            //var_dump($idnumber);exit;
          //var_dump($data['check']);exit;
            /*$checkid=DB::name('role')->where('name',$data['check'])->column('name');

            var_dump($checkid);exit;*/
            if(is_null($job_num)){
                if(!empty($data['depart'])){
                    if(!empty($data['duty'])){
                        if(!empty($data['user_login'])){
                            if(!empty($data['user_email']) && is_null($user_email)){
                                if(!empty($data['idnumber']) && is_null($idnumber)){
                                    if(!empty($data['birthday'])){
                                        if(!empty($data['user_pass'])){
                                            if(!empty($data['mobile']) && is_null($mobile)){
                                                if(!empty($data['address'])){
                                                    if(!empty($data['storeid'])){
                                                       $dat= DB::name('store')->where('store_name',$data['storeid'])->field('id')->find();

                                                        $data['storeid']=$dat['id'];
                                                        //var_dump($data['storeid']);exit;
                                                        //var_dump($dataa);exit;
                                                        if(!empty($data['check'])){

                                                          $user['depart']=$data['depart'];
                                                            $user['duty']=$data['duty'];
                                                            $user['user_login']=$data['user_login'];
                                                            $user['job_num']=$data['job_num'];
                                                            $user['user_email']=$data['user_email'];

                                                            $user['idnumber']=$data['idnumber'];
                                                            $user['birthday']=$data['birthday'];
                                                            $user['birthday']=strtotime($user['birthday']);
                                                            //var_dump( $user['birthday']);exit;
                                                            $user['user_pass']=$data['user_pass'];
                                                            $user['user_pass']=cmf_password( $user['user_pass']);
                                                            $user['mobile']=$data['mobile'];
                                                            $user['address']=$data['address'];
                                                            $user['storeid']=$data['storeid'];
                                                            $user['create_time']=time();


                                                            if($data['sex']=='男'){
                                                                $data['sex']=1;
                                                            }elseif($data['sex']=='女'){
                                                                $data['sex']=2;
                                                            }
                                                            $user['sex']=$data['sex'];

                                                            $party=new PartyModel;
                                                           $aa=$party->save($user);


                                                            if($aa){
                                                                //echo"chengong1";exit;
                                                                //$Roleuser=new RoleuserModel;
                                                                $user_id=DB::name('user')->where('idnumber',$user['idnumber'])->field('id')->find();


                                                                $role_id['role_id']=$data['check'];
                                                               //$cc['role_id']=$role_id;
                                                                $cc['user_id']=$user_id['id'];

                                                                //$Roleuser->save($cc);
                                                                foreach($role_id['role_id'] as $v){
                                                                    Db::name('role_user')->insert(["role_id" => $v, "user_id" => $cc['user_id']]);

                                                                }
                                                                $this->success('数据提交成功');
                                                                //var_dump($role_id['role_id']);exit;




                                                            }else{
                                                                echo"提交失败";
                                                            }










                                                        }else{
                                                            $this->error('角色不能为空');
                                                        }

                                                    }else{
                                                        $this->error('店铺名称不能为空');
                                                    }

                                                }else{
                                                    $this->error('地址不能为空');
                                                }

                                            }else{
                                                $this->error('手机号不能为空');
                                            }

                                        }else{
                                            $this->error('密码不能为空');
                                        }

                                    }else{
                                        $this->error('生日不能为空');
                                    }

                                }else{
                                    $this->error('身份证号错误');
                                }

                            }else{
                                $this->error('邮箱错误');
                            }

                        }else{
                            $this->error('姓名不能为空');
                        }

                    }else{
                        $this->error('职务不能为空');
                    }

                }else{
                    //echo"<script>alert('部门能为空')</script>";
                    $this->error('部门不能为空');
                }
            }else{
                $this->error('工号不能重复');
            }




        }

    }
    function status(){
        $param = $this->request->param();
        $param=$param['id'];
        //var_dump($param);exit;
        if($param=='1'){

            $this->error('此为系统默认，不能操作');
        }else{

            $user_status=DB::name('user')->where('id',$param)->field('user_status')->find();
            //var_dump($user_status);exit;
            if($user_status['user_status']=='1'){
                //echo 123;exit;
                $data['user_status']=0;
               $user=new PartyModel;
                $user->where('id',$param)->setField($data);
                $this->success('成功');
            }elseif($user_status['user_status']=='0'){
                $data['user_status']=1;
                $user=new PartyModel;
                $user->where('id',$param)->setField($data);
                $this->success('成功');
            }
        }
    }

    function edit(){
        $param = $this->request->param();
        $param=$param['id'];
        if($param=='1'){
            $this->error('此为系统默认，不能操作');
        }else{
            $name=DB::name('role')->select();
            //var_dump($name);exit;

            $this->assign('name',$name);
            $role_ids = DB::name('role_user')->where(["user_id" => $param])->column("role_id");
            $this->assign("role_ids", $role_ids);
            $user=DB::name('user')->where('id',$param)->select();
            $this->assign('user',$user);

            $job=DB::name('job')->field('depart')->select();
            $this->assign('job',$job);

            $job1=DB::name('job')->field('duty')->select();
            $this->assign('job1',$job1);

        }
       return $this->fetch();


    }


    function editPost(){
        if($this->request->isPost()){
            //session_start();
            $data=$this->request->Post();
            //var_dump($data);exit;
            if(!empty($data['user_pass'])){
                $param = $this->request->param();
                $param=$param['id'];
                $user['depart']=$data['depart'];
                $user['duty']=$data['duty'];
                $user['user_login']=$data['user_login'];
                $user['job_num']=$data['job_num'];
                $user['user_email']=$data['user_email'];

                $user['idnumber']=$data['idnumber'];
                $user['birthday']=$data['birthday'];
                $user['birthday']=strtotime($user['birthday']);
                //var_dump( $user['birthday']);exit;
                $user['user_pass']=$data['user_pass'];
                $user['user_pass']=cmf_password( $user['user_pass']);
                $user['mobile']=$data['mobile'];
                $user['address']=$data['address'];
                $user['storeid']=$data['storeid'];
                $user['create_time']=time();


                if($data['sex']=='男'){
                    $data['sex']=1;
                }elseif($data['sex']=='女'){
                    $data['sex']=2;
                }
                $user['sex']=$data['sex'];

               $aa= DB::name('user')->where('id',$param)->update($user);
                if($aa){
                    DB::name('role_user')->where('user_id',$param)->delete();
                    $role_id['role_id']=$data['check'];
                    foreach($role_id['role_id'] as $v){
                       Db::name('role_user')->insert(["role_id" => $v, "user_id" => $param]);


                    }
                }else{
                    $this->error('失败');
                }

                $this->success('更新成功',url('personnel/add'));



            }else{
                $this->error('密码不能为空');
            }



            //var_dump($data);exit;



        }




        return $this->fetch();
    }

    function del(){
        $param = $this->request->param();
        $param=$param['id'];
        if($param=='1'){
            $this->error('此为系统默认，不能操作');
        }else{
            DB::name('user')->where('id',$param)->delete();
            $this->success('删除成功');
        }
    }

    function job(){
        if($this->request->isPost()){
            //session_start();
            $data=$this->request->Post();
           $job=DB::name('job')->where('depart',$data['depart'])->find();
            $job1=DB::name('job')->where('duty',$data['duty'])->find();
            if(!empty($data['depart']) && is_null($job) || is_null($job1)){
                if(!empty($data['duty'])){
                    $user=new JobModel;
                    $user->save($data);

                    $this->success('添加成功');
                }else{
                    $this->error('职务不能为空');
                }

            }else{
                $this->error('已有此职位');
            }

        //var_dump($job);exit;

        }


        return $this->fetch();
    }





}
























